#include "MainWindow.h"
#include "MainWindowPrivate.h"
#include "MainViewer.h"
#include "Units.h"
#include "NodeData.h"
#include <QtGUI/QDirModel>
#include <QtGUI/QMessageBox>
#include <QtGUI/QFileDialog>


MainWindow::MainWindow(void)
{
	d = new Private;
	d->document = new Document;
	d->Ui.setupUi(this);

	// path view
//	QDirModel* pModel = new QDirModel;
//	d->Ui.resourceTreeView->setModel( pModel );
//	QString dataPath( QDir::current().filePath("data") );
//	d->Ui.resourceTreeView->setRootIndex( pModel->index(dataPath) );

	// scene list view
	d->Ui.sceneListView->setModel( d->document->getModel() );

	// main view
	d->viewer = new MainViewer;
	setCentralWidget(d->viewer);
	d->viewer->setModel( d->document->getModel() );
	d->viewer->setItemSelectionModel( d->Ui.sceneListView->selectionModel() );

    connect(d->Ui.widthBox, SIGNAL(valueChanged(int)), SLOT(gridDimensionsChanged()));
    connect(d->Ui.heightBox, SIGNAL(valueChanged(int)), SLOT(gridDimensionsChanged()));

    connect(d->Ui.sceneListView->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), SLOT(updatePropertiesGUI(QItemSelection)));

    connect(d->Ui.scaleSpinBox, SIGNAL(valueChanged(double)), SLOT(setUniformScaleOfSelectedObjects(double)));

	updateGrid();
}


MainWindow::~MainWindow(void)
{
	delete d;
}


void MainWindow::gridDimensionsChanged()
{
	updateGrid();
}


void MainWindow::open()
{
	//QString filePath = QFileDialog::getOpenFileName(this, tr("Open Scene"),	"",	tr("Scene Files (*.osg *.ive)") );
	//if(!filePath.isEmpty())
	//{
	//	viewer->openScene(filePath);
	//	Ui.widthBox->setValue(viewer->getGridDimensions().x());
	//	Ui.heightBox->setValue(viewer->getGridDimensions().y());
	//}
}


void MainWindow::save()
{
	//viewer->saveScene("saved_scene.osg");
}


void MainWindow::saveAs()
{
}


void MainWindow::deleteSelected()
{
    foreach(QModelIndex selected, d->Ui.sceneListView->selectionModel()->selectedIndexes())
    {
        osg::Node* selectedNode = d->document->getModel()->node(selected);
        d->document->getModel()->removeObject(SceneManager::Model, selectedNode);
    }
}


void MainWindow::updateGrid()
{
    int w = d->Ui.widthBox->value();
	int h = d->Ui.heightBox->value();
	d->document->getHomeGrid()->setDimensions(osg::Vec2s(w, h));
}


void MainWindow::updatePropertiesGUI(QItemSelection selected)
{
    if(selected.isEmpty())
    {
        d->Ui.propertiesDockWidget->setEnabled(false);
    }
    else
    {
        d->Ui.propertiesDockWidget->setEnabled(true);
        d->Ui.frozenCheckBox->setChecked(!nodeData(selected.indexes().at(0))->selectable);
        d->Ui.scaleSpinBox->setValue(patNode(selected.indexes().at(0))->getScale().x());
    }
}


void MainWindow::setUniformScaleOfSelectedObjects(double s)
{
    foreach(QModelIndex selected, d->Ui.sceneListView->selectionModel()->selectedIndexes())
    {
        if(nodeData(selected)->selectable)
        {
            patNode(selected)->setScale(osg::Vec3(s,s,s));
        }
    }
}


NodeData* MainWindow::nodeData(QModelIndex index) const
{
    if(index.isValid())
    {
        return dynamic_cast<NodeData*>(d->document->getModel()->node(index)->getUserData());
    }
    return NULL;
}


osg::PositionAttitudeTransform* MainWindow::patNode(QModelIndex index) const
{
    if(index.isValid())
    {
        return dynamic_cast<osg::PositionAttitudeTransform*>(d->document->getModel()->node(index));
    }
    return NULL;
}
